import { Browser as WeaBrowser } from 'weaver-mobile-page';

import { isArray } from 'lodash';
import { toJS } from 'mobx';
import { downPngImg, delPngImg, requiredPngImg } from '.';

export default class WeaCityPickerCellType extends window.GC.Spread.Sheets.CellTypes.Base {
    typeName = 'WeaCityPickerCellType';
    fieldType;
    browserType;
    browserTitile;
    dataParams = {};
    tabs = [];
    viewAttr = 2;
    label;
    open = false;
    width;
    ctx;
    x;
    y;
    w;
    h;
    clickwidth;
    expandType;
    combineSameCell;
    paint(ctx, value, x, y, w, h, style, context) {
        if (!ctx) {
            return;
        }
        this.ctx = ctx;
        this.x = x;
        this.y = y;
        this.w = w;
        this.h = h;
        if (this.viewAttr == 1) {
            super.paint(ctx, value, x, y, w, h, style, context);
        } else if(this.viewAttr == 3) {
            super.paint(ctx, value, x, y, w - 20, h, style, context);
            super.paint(ctx, '', x + w - 20, y, 20, h, style, context);
            const width = Math.min(16, h);
            if (value && value[0] && value[0].name) {
                this.clickwidth = 20;
                ctx.drawImage(downPngImg, x + w - this.clickwidth, y + (h - width) / 2, width, width);
                const textwidth = parseInt(ctx.measureText(value[0].name).width.toString()) + 3;
                // 避免越界
                if (textwidth <= (w - 20 - 8)) {

                    this.initDelBtn(textwidth);
                }
            } else {
                this.clickwidth = 20 + 9;
                ctx.drawImage(downPngImg, x + w - this.clickwidth, y + (h - width) / 2, width, width);
                ctx.drawImage(requiredPngImg, x + w - 8, y + (h - 7) / 2, 7, 7);
            }

        } else {
            super.paint(ctx, value, x, y, w - 20, h, style, context);
            super.paint(ctx, '', x + w - 20, y, 20, h, style, context);
            const width = Math.min(16, h);
            
            this.clickwidth = 20;
            ctx.drawImage(downPngImg, x + w - this.clickwidth, y + (h - width) / 2, width, width);
            if (value && value[0] && value[0].name) {
                const textwidth = parseInt(ctx.measureText(value[0].name).width.toString()) + 3;
                // 避免越界
                if (textwidth <= (w - 20 - 8)) {

                    this.initDelBtn(textwidth);
                }
            }
        }
    }
    format(value, format, formattedData, context) {
        if (isArray(value)) {
            return value.filter(d => typeof d !== 'function').map(d => d.names || d.name).join(',');
        } else {
            return '';
        }
    }
    fromJSON(setting) {
        this.fieldType = toJS(setting.fieldType);
        this.viewAttr = setting.viewAttr;
        this.label = setting.label;
        this.expandType = setting.expandType;
        this.combineSameCell = setting.combineSameCell;
        this.browserType = this.fieldType[1].value;
        if (this.fieldType.length == 2) {
            this.browserTitile = this.fieldType[1].valueSpan;
        } else {
            this.browserTitile = this.fieldType[1]["valueObj"][0].name;
        }
    }
    getHitInfo(x, y, cellStyle, cellRect, context) {
        const { row, col, sheetArea, sheet } = context;
        return {
            x,
            y,
            row,
            col,
            cellRect,
            sheetArea,
            sheet,
        };
    }
    processMouseUp(hitInfo) {
        const { x, y, cellRect, sheet, row, col, sheetArea, reservedLocation, isReservedLocation } = hitInfo;
        
        const value = sheet.getCell(row, col).value();
        let cwidth = 20;
        if(this.viewAttr == 3 && !(value && value[0] && value[0].name)) {
            cwidth = 29;
        }
        console.log(cwidth)
        if (x && cellRect) {
            if (this.viewAttr !== 1 && x >= (cellRect.x + cellRect.width - cwidth)) {
                this.showBrowserModal(sheet.getCell(row, col, sheetArea), cellRect);
            } else if (this.viewAttr !== 1 && this.ctx) {
                if (!value) {
                    return true;
                }
                const width = parseInt(this.ctx.measureText(value[0].name).width.toString()) + 3;
                if (width && x >= (cellRect.x + width) && x <= (cellRect.x + width + 12)) {
                    sheet.getCell(row, col).value("");
                }
                return true;
            }
        }
        return super.processMouseUp(hitInfo);
    }

    showBrowserModal(context, cellRect) {
        // 只读 不展示
        if (this.viewAttr && this.viewAttr == 1) {
            return;
        }
        const { sheet, row, col } = context;

        
        this.switchValue(sheet.getCell(row, col).value());

        window.browserAllBrowserStore.currentCell = sheet.getCell(row, col);
        window.browserAllBrowserStore.currentValue = sheet.getCell(row, col).value();
        window.browserAllBrowserStore.type = this.browserType;
        this.showBytype();
        
    }

    showBytype() {
        this.browserType;

        switch (this.browserType) {

            case '1':
            case '17':
            window.excelHistory.push(window.browserAllBrowserStore.browserHrm.path);
            break;
            case '258':
            window.excelHistory.push(window.browserAllBrowserStore.browserCitygj.path);
            break;
            case '2222':
            window.excelHistory.push(window.browserAllBrowserStore.browserCitysf.path);
            break;
            case '58':
            window.excelHistory.push(window.browserAllBrowserStore.browserCitycs.path);
            break;
            case '263':
            window.excelHistory.push(window.browserAllBrowserStore.browserCityqx.path);
            break;
            default:
            window.excelHistory.push(window.browserAllBrowserStore.browser.path);
            break;

        }

        
    }

    switchValue(value) {
        switch (this.browserType) {

            case '178':
                window.browserAllBrowserStore.setDafalutValue((value || []).map(d => ({ randomFieldId: d.id, yearName: this.delHtmlTag(d.name) })));
            default:
                window.browserAllBrowserStore.setDafalutValue((value || []));
        }
    }
}